package cn.edu.nyist.controller;

import cn.edu.nyist.entity.Region;
import cn.edu.nyist.service.RegionService;
import cn.edu.nyist.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (Region)表控制层
 *
 * @author makejava
 * @since 2021-03-21 17:55:26
 */
@Controller
@RequestMapping("region")
public class RegionController {

    private static Logger log = LoggerFactory.getLogger(RegionController.class);

    @Autowired
    private AjaxResult ajaxResult;

    @Autowired
    private RegionService regionService;


    /**
     * 跳转地区页面
     * @return
     */
    @GetMapping("/regionIndex")
    public String regionIndex(){
        return "yju/region/regionList";
    }

    @GetMapping("queryAllRegion")
    @ResponseBody
    public List<Region> queryAllRegion(){
        return regionService.queryAllRegion();
    }

    /**
     * 异步加载地区列表
     * @param pageno
     * @param pagesize
     * @param regionName
     * @return
     */
    @RequestMapping("/regionList")
    @ResponseBody
    public Object regionList(@RequestParam(value = "page", defaultValue = "1") Integer pageno,
                           @RequestParam(value = "limit", defaultValue = "5") Integer pagesize,
                           String regionName ){

        Map<String,Object> paramMap = new HashMap();
        paramMap.put("pageno",pageno);
        paramMap.put("pagesize",pagesize);

        //判断是否为空
        if(!StringUtils.isEmpty(regionName)) {
            paramMap.put("regionName",regionName);
        }
        PageBean<Region> pageBean = regionService.queryPage(paramMap);
        Map<String,Object> rest = new HashMap();
        rest.put("code", 0);
        rest.put("msg", "");
        rest.put("count",pageBean.getTotalsize());
        rest.put("data", pageBean.getDatas());
        return rest;
    }


    /**
     * 跳转添加地区页面
     * @return
     */
    @GetMapping("/addRegion")
    public String addRegion(String type, Integer id, Model model){
        if(type != null && type.equals("edit")){
            Region  region = regionService.selectById(id);
            model.addAttribute(Const.REGION,region);
        }
        return "yju/region/addRegion";
    }

    /**
     * 添加地区  修改地区
     * @param region
     * @return
     */
    @PostMapping("/addRegion")
    @ResponseBody
    public AjaxResult submitAddRegion(Region region,HttpSession session){
        Region byRegionName = regionService.selectByRegionName(region.getRegionName());
        if(region.getId() !=null){
            //修改地区
            if(byRegionName != null && !region.getId().equals(byRegionName.getId())){
                ajaxResult.ajaxFalse("该地区已存在");
                return ajaxResult;
            }

            int count = regionService.editByRegion(region);
            if(count > 0){
                ajaxResult.ajaxTrue("修改成功");
            }else{
                ajaxResult.ajaxFalse("修改失败");
            }
        }else{
            //添加地区
            if(byRegionName != null){
                ajaxResult.ajaxFalse("该地区已存在");
                return ajaxResult;
            }
            int count = regionService.insertRegion(region);
            if(count > 0){
                ajaxResult.ajaxTrue("添加成功");
                session.setAttribute(Const.REGION,null);
            }else{
                ajaxResult.ajaxFalse("添加失败");
            }
        }
        return ajaxResult;
    }

    /**
     * 删除地区
     * @param data
     * @return
     */
    @PostMapping("/delRegion")
    @ResponseBody
    public AjaxResult delRegion(Data data, HttpSession session){
        int count = regionService.delByRegionIds(data.getIds());
        if(count >= data.getIds().size()){
            ajaxResult.ajaxTrue("删除成功");
            session.setAttribute(Const.REGION,null);
        }else{
            ajaxResult.ajaxFalse("删除失败");
        }
        return ajaxResult;
    }
}